# @Time : 2025-01-11 16:37
# @Author : Fioman 
# @Phone : 13149920693
import threading

from common.produce_logger import debug_produce
from drivers.plc_modbus_base import PlcModbusBase
from personal_tool.data_result import DataResult

"""
收板机plc类,单例类
"""


class LoaderPlc(PlcModbusBase):
    _lock = threading.Lock()
    _instance = None
    _initFlag = False

    def __new__(cls, *args, **kwargs):
        if cls._instance is None:
            with cls._lock:
                cls._instance = super().__new__(cls)
        return cls._instance

    def __init__(self):
        if not self._initFlag:
            self.ip = "192.168.6.11"
            self.port = 502
            self._initFlag = True
            super().__init__(self.ip, self.port)
            debug_produce(f"LoaderPlc.__init__() 初始化成功")

    def write_board_length(self, length: int) -> DataResult:
        """
        写入板长
        @ param length: 板长
        """
        writeAddress = 690
        return self.write_word(writeAddress, length)

    def check_connect(self) -> DataResult:
        """
        检查PLC是否在线
        """
        readAddress = 690
        return self.read_word(readAddress)
